package com.ctvit.sjk.knowledge.app.controller;

import com.ctvit.sjk.knowledge.app.pojo.dto.AppChatMessageDTO;
import com.ctvit.sjk.knowledge.app.pojo.vo.AppAuthenticationVO;
import com.ctvit.sjk.knowledge.app.service.ITabAppChatService;
import com.ctvit.sjk.knowledge.core.controller.KnowledgeController;
import com.ctvit.sjk.knowledge.core.page.TableRecordInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.Collections;

/**
 * 应用聊天Controller
 *
 * @author heyingcheng
 * @date 2025/3/22
 */
@Slf4j
@RestController
@RequestMapping("knowledge/application")
public class TabAppChatController extends KnowledgeController {

    private final ITabAppChatService tabAppChatService;

    public TabAppChatController(ITabAppChatService tabAppChatService) {
        this.tabAppChatService = tabAppChatService;
    }

    /**
     * 查询应用聊天列表
     */
    @GetMapping("{appId}/chat/{pageNum}/{pageSize}")
    public TableRecordInfo list(@PathVariable String appId,
                                @PathVariable int pageNum,
                                @PathVariable int pageSize,
                                @RequestParam("start_time") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
                                @RequestParam("end_time") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime,
                                @RequestParam("min_star") Integer minStar,
                                @RequestParam("min_trample") Integer minTrample,
                                @RequestParam String comparer,
                                @RequestParam("abstract") Integer chatDesc) {
        return getRecordTable(Collections.emptyList(), pageNum, pageSize);
    }

    /**
     * 查询应用聊天客户端列表
     */
    @GetMapping("{appId}/chat/client/{pageNum}/{pageSize}")
    public TableRecordInfo list(@PathVariable String appId,
                                @PathVariable int pageNum,
                                @PathVariable int pageSize) {
        return getRecordTable(Collections.emptyList(), pageNum, pageSize);
    }

    /**
     * 开启应用聊天
     */
    @GetMapping("{appId}/chat/open")
    public AjaxResult open(HttpServletRequest request,
                           @PathVariable String appId) {
        AppAuthenticationVO appAuthenticationVO = parseAppAuthentication(request);
        return toAjax(tabAppChatService.open(appAuthenticationVO.getApplicationId(), appAuthenticationVO.getClientId(), CREATE_ID));
    }

    /**
     * 开启应用聊天
     */
    @PostMapping("chat_message/{chatId}")
    public AjaxResult message(@PathVariable String chatId, @RequestBody AppChatMessageDTO dto) {
        log.info("chatId:[{}], dto:[{}]", chatId, dto);
        return success(tabAppChatService.message(chatId, dto, CREATE_ID));
    }

}
